package com.fg.cloud.factory.controller.factory;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fg.cloud.common.PageUtils;
import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.common.validate.LongUtils;
import com.fg.cloud.framework.controller.BaseController;
import com.fg.cloud.manager.entry.factory.FactoryStatementDetails;
import com.fg.cloud.manager.entry.factory.FactoryStatementEntity;
import com.fg.cloud.manager.service.factory.FactoryStatementService;

/**
 * Description:厂商对账单controller
 * 
 * @author shaoqiping
 * @date 2017年8月28日下午4:28:47
 */
@Controller
@RequestMapping("/statement")
public class FactoryStatementController extends BaseController {

	@Autowired
	private FactoryStatementService statementService;
	
	/**
	 * Description:生成对账单
	 * 
	 * @param request
	 * @param response
	 * @param statementEntity
	 * @author shaoqiping
	 * @date 2017年8月29日下午5:34:33
	 */
	@RequestMapping("/createStatement")
	@ResponseBody
	public void createStatement(HttpServletRequest request, HttpServletResponse response, String startTime,
			String endTime, Long ageId, String remark,String name) {
		try {
			FactoryStatementEntity statementEntity = new FactoryStatementEntity();
			statementEntity.setAgeId(ageId);
			statementEntity.setRemark(remark);
			statementEntity.setName(name);
			statementEntity.setFacId(getCurrentFactoryUser().getFacId());

			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			statementEntity.setStartTime(format.parse(startTime+" 00:00:00"));
//			Calendar cal=Calendar.getInstance();
//			cal.setTime(format.parse(endTime));
//			cal.add(Calendar.DATE,1);
			statementEntity.setEndTime(format.parse(endTime+" 23:59:59"));
			boolean flag = statementService.createStatement(statementEntity);
			if (flag) {
				doResult(response, true, StatusCode.SAVESUCCESS, null, ResultType.ADD_SUCCESS);
			} else {
				doResult(response, false, StatusCode.SAVEERROR, null, ResultType.ADD_FAULT);
			}
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}

	@RequestMapping("/checkTime")
	@ResponseBody
	public void checkTime(HttpServletRequest request, HttpServletResponse response, String startTime, String endTime,
			Long ageId, String remark,String name) {
		try {
			if (ageId == null || LongUtils.isEmpty(ageId) || startTime == null || startTime == null) {
				doResult(response, false, StatusCode.LACKVALUE, null, "请填写相关数据信息");
				return;
			}
			FactoryStatementEntity statementEntity = new FactoryStatementEntity();
			statementEntity.setAgeId(ageId);
			statementEntity.setRemark(remark);
			statementEntity.setFacId(getCurrentFactoryUser().getFacId());

			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			statementEntity.setStartTime(format.parse(startTime+" 00:00:00"));
//			Calendar cal=Calendar.getInstance();
//			cal.setTime(format.parse(endTime+"23:59:59"));
			statementEntity.setEndTime(format.parse(endTime+" 23:59:59"));

			FactoryStatementEntity oldEntity = statementService.findLastOne(statementEntity.getFacId(),
					statementEntity.getAgeId());
			if (oldEntity != null) {
				if (oldEntity.getEndTime().getTime()>statementEntity.getStartTime().getTime()) {
					doResult(response, false, StatusCode.OPERATIONERROR, null, "开始时间必须大于上一期截止时间");
					return;
				}
				if ((statementEntity.getStartTime().getTime() - oldEntity.getEndTime().getTime())
						>24 * 3600 * 1000) {
					doResult(response, false, StatusCode.SAVEERROR, null, "与上一期截止时间相差超过一天");
					return;
				}
			}
			createStatement(request, response, startTime, endTime,
					statementEntity.getAgeId(), statementEntity.getRemark(),name);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}
	
	
	/**
	  * Description:推送对账单
	  * @param request
	  * @param response
	  * @param entity
	  * @author shaoqiping
	  * @date 2017年10月12日上午10:15:59
	*/
	@RequestMapping("/push")
	@ResponseBody
	public void push(HttpServletRequest request, HttpServletResponse response, FactoryStatementEntity entity){
		try {
			if(entity==null || LongUtils.isEmpty(entity.getId())){
				doResult(response, false, StatusCode.LACKVALUE, null, "请选择要推送的对账单");
				return;
			}
			entity.setAgeId(entity.getAgeId());
			entity.setFacId(getCurrentFactoryUser().getFacId());
			boolean flag = statementService.modify(entity);
			if(flag){
				doResult(response, true, StatusCode.SAVESUCCESS, null, "推送成功");
			}else{
				doResult(response, false, StatusCode.SAVEERROR, null, "推送失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}
	
	/**
	  * Description:删除指定未确认对账单
	  * @param request
	  * @param response
	  * @param id
	  * @author shaoqiping
	  * @date 2017年10月12日上午10:26:52
	*/
	@RequestMapping("/delete")
	@ResponseBody
	public void delete(HttpServletRequest request, HttpServletResponse response, Long id){
		try {
			if(id==null || LongUtils.isEmpty(id)){
				doResult(response, false, StatusCode.LACKVALUE, null, "请选择要删除的对账单");
				return;
			}
			boolean flag = statementService.remove(id);
			if(flag){
				doResult(response, true, StatusCode.SAVESUCCESS, null, "删除成功");
			}else{
				doResult(response, false, StatusCode.SAVEERROR, null, "删除失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}
	
	/**
	 * Description:查询对账单详情
	 * 
	 * @param request
	 * @param response
	 * @param statementEntity
	 * @author shaoqiping
	 * @date 2017年8月29日下午3:19:33
	 */
	@RequestMapping("/findDetails")
	@ResponseBody
	public void findDetails(HttpServletRequest request, HttpServletResponse response, Long id) {
		try {
			if (id == null || LongUtils.isEmpty(id)) {
				doResult(response, false, StatusCode.LACKVALUE, null, "请选择相应对账单");
				return;
			}
			List<FactoryStatementDetails> details = statementService.findDetails(id,
					getCurrentFactoryUser().getFacId());
			if (details == null) {
				doResult(response, false, StatusCode.FINDERROR, null, ResultType.FIND_FAULT);
				return;
			}
			doResult(response, true, StatusCode.FINDSUCCESS, details, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}

	/**
	 * Description:条件分页查询
	 * 
	 * @param request
	 * @param response
	 * @param page
	 * @param rows
	 * @param sort
	 * @param order
	 * @param statementEntity
	 * @author shaoqiping
	 * @date 2017年8月29日下午3:02:29
	 */
	@RequestMapping("/findList")
	@ResponseBody
	public void findList(HttpServletRequest request, HttpServletResponse response, Integer page, Integer rows,
			String sort, String order, String startTime, String endTime, Long ageId, Integer status,String statementNum) {
		try {
			if (ageId == null || LongUtils.isEmpty(ageId)) {
				doResult(response, false, StatusCode.LACKVALUE, null, "请选择经销商");
				return;
			}
			// page默认为1 rows默认为10
			if (page == null || page <= 0) {
				page = 1;
			}
			if (rows == null || rows <= 0) {
				rows = 10;
			}
			PageUtils<FactoryStatementEntity> pageinfo = new PageUtils<FactoryStatementEntity>(page, rows, sort, order);
			Map<String, Object> condition = pageinfo.getCondition();
			condition.put("facId", getCurrentFactoryUser().getFacId());
			condition.put("ageId", ageId);
			condition.put("startTime", startTime);
			condition.put("endTime", endTime);
			condition.put("statementNum", statementNum);
			condition.put("status", status);
			pageinfo.setCondition(condition);

			statementService.findList(pageinfo);
			doResult(response, true, StatusCode.FINDSUCCESS, pageinfo, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}
	
	/**
	  * Description:查询对账单还款详情
	  * @param request
	  * @param response
	  * @param id
	  * @author shaoqiping
	  * @date 2017年10月13日下午2:39:58
	*/
	@RequestMapping("/findPayments")
	@ResponseBody
	public void findPayments(HttpServletRequest request, HttpServletResponse response,Long id){
		try {
			if (id == null || LongUtils.isEmpty(id)) {
				doResult(response, false, StatusCode.LACKVALUE, null, "请选择相应流水");
				return;
			}
			doResult(response, true, StatusCode.FINDSUCCESS, statementService.findPayments(id), ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}
}
